
package com.companymodule.servlet;
import com.companymodule.bean.TbCompanyProductTypeMiddle;
import com.companymodule.dao.*;
import com.productmodule.dao.TbProductTypeDAO;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.List;

import util.*;

public class TbCompanyProductTypeMiddleServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        PrintWriter out = response.getWriter();
        if("add".equals(method)){
        int companyId=Integer.parseInt(request.getParameter("companyId"));
        int productTypeId=Integer.parseInt(request.getParameter("productTypeId"));
            if(new TbCompanyProductTypeMiddleDAO().add(new TbCompanyProductTypeMiddle(companyId,productTypeId))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
            goList(request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_CompanyProductTypeMiddle", "CompanyProductTypeMiddleId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);
        }
        if("edit".equals(method)){
        int companyProductTypeMiddleId=Integer.parseInt(request.getParameter("companyProductTypeMiddleId"));
        int companyId=Integer.parseInt(request.getParameter("companyId"));
        int productTypeId=Integer.parseInt(request.getParameter("productTypeId"));
            if(new TbCompanyProductTypeMiddleDAO().edit(new TbCompanyProductTypeMiddle(companyProductTypeMiddleId,companyId,productTypeId))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        if ("list".equals(method))goList(request, response);
        if ("listById".equals(method)) {
            request.getSession(true).setAttribute("tbCompanyProductTypeMiddle",new TbCompanyProductTypeMiddleDAO().getTbCompanyProductTypeMiddleByCompanyProductTypeMiddleId(Integer.parseInt(request.getParameter("companyProductTypeMiddleId"))));
            response.sendRedirect("CompanyModule/TbCompanyProductTypeMiddleOperate.jsp");
    	}
        if("showadd".equals(method)){
            request.getSession(true).removeAttribute("tbCompanyProductTypeMiddle");
            response.sendRedirect("CompanyModule/TbCompanyProductTypeMiddleOperate.jsp");
        }
        if("getObj".equals(method)){
	        	List<TbCompanyProductTypeMiddle>midList = new TbCompanyProductTypeMiddleDAO(). getTbCompanyProductTypeMiddleByProductTypeId(Integer.parseInt(request.getParameter("productTypeId").equals("")?"0":request.getParameter("productTypeId")),Integer.parseInt(request.getParameter("companyId")));
        	int num = 0;
        	if(midList!=null&&midList.size()>0){
        		num = midList.size();
        	}
        	out.print(num);
        }
        if("getCompanyProductMaxTypeByCompanyId".equals(method)){
        	List<TbCompanyProductTypeMiddle> cptList=new TbCompanyProductTypeMiddleDAO().getProductTypeMiddleByIsMaxAndCompanyId(0,Integer.parseInt(request.getParameter("companyId")));
        	String str="";
        	for(TbCompanyProductTypeMiddle cpt:cptList){
        		str="".equals(str)?cpt.getProductTypeId()+":"+cpt.getTbProductType().getProductTypeName():str+"#"+cpt.getProductTypeId()+":"+cpt.getTbProductType().getProductTypeName();
        	}
        	out.println(str);
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession(true).setAttribute("tbCompanyList",new TbCompanyDAO().getTbCompanyAll());
    	request.getSession().setAttribute("tbProductTypeList",new TbProductTypeDAO().getTbProductTypeAll());
    	request.getSession(true).setAttribute("tbCompanyProductTypeMiddleList",new TbCompanyProductTypeMiddleDAO().getTbCompanyProductTypeMiddleAll());
        response.sendRedirect("CompanyModule/TbCompanyProductTypeMiddleList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
